const { getConfig } = require('../config')
const md5 = require('js-md5')
const Storage = require('./Storage')

function getSign() {
    let {sign_key } = getConfig()
    let signObj = {
        // "X-Auth-Ts": parseInt(new Date() / 1000),
        // "X-Auth-Noncestr": randomString(8),
    }
    let sarr = Object.keys(signObj).sort((a, b) => { return a.toLowerCase() > b.toLowerCase() ? 1 : -1; }).map((key, index) => {
        return `${index ? '&' : ''}${key.toLowerCase()}=${signObj[key]}`
    }).join('')
    // signObj['X-Auth-Sign'] = md5(sarr + sign_key).slice(5, 15)
    signObj['token'] = Storage.get('token')
    // signObj['X-Mumway-Traceid'] = randomString(32)
    // signObj['X-Trace-ID'] = randomString(32)
    return signObj
}

function randomString(len) {
    len = len || 32;
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    var maxPos = $chars.length;
    var pwd = '';
    for (let i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}
module.exports = getSign
